package com.smartao.core.license;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 生成密钥对
 * @author Ricky
 *
 */
public class SmartaoKeyPair {
	private PrivateKey priKey;
	private PublicKey pubKey;

	private static final Logger logger = LoggerFactory.getLogger(SmartaoKeyPair.class);

	/**
	 * 初始化key pair
	 */
	public SmartaoKeyPair(String seed) {
		this.gengerateKeyPair(seed);
	}

	public void gengerateKeyPair(String seed) {
		try {
			KeyPairGenerator keygen = KeyPairGenerator.getInstance(SecurityConstant.KEY_ALGORITHM);
			keygen.initialize(SecurityConstant.KEY_SIZE);
			SecureRandom secrand = SecureRandom.getInstance(SecurityConstant.RNG_ALGORITHM, SecurityConstant.RNG_PROVIDER);
			secrand.setSeed(seed.getBytes());
			keygen.initialize(1024, secrand);
			KeyPair keys = keygen.genKeyPair();

			pubKey = keys.getPublic();
			priKey = keys.getPrivate();

		} catch (Exception e) {
			logger.error("生成密钥对失败", e);
		}

	}

	public PrivateKey getPriKey() {
		return priKey;
	}

	public PublicKey getPubKey() {
		return pubKey;
	}
}